package com.hotacorp.opencloud.authserver.service.impl;


import java.awt.image.BufferedImage;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.code.kaptcha.Producer;
import com.hotacorp.opencloud.authserver.service.SysCaptchaService;

/**
 * 验证码实现类
 *
 * @author lwg
 */
@Service
public class SysCaptchaServiceImpl implements SysCaptchaService {
	private static final String VALIDATE_CODE_OAUTH_KEY = "validateCode:oauth";
    @Autowired
    private Producer producer;
	@Autowired
	StringRedisTemplate stringRedisTemplate;

    @Override
    @Transactional(rollbackFor={Exception.class})
    public BufferedImage getCaptcha(String uuid) {
        if(StringUtils.isBlank(uuid)){
            throw new RuntimeException("uuid不能为空");
        }
        //生成文字验证码
        String code = producer.createText();
        
        stringRedisTemplate.boundValueOps(VALIDATE_CODE_OAUTH_KEY + ":" + uuid).set(code, 5, TimeUnit.MINUTES);;

        return producer.createImage(code);
    }

}
